
package net.lab0.fractal.gui;


import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;


public class BuddhabrotDataSourceGeneratorFrame
extends JFrame
{
    private JPanel contentPane;
    private File   outputFolder;
    private JLabel lblPath;
    private JLabel lblConvert;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    BuddhabrotDataSourceGeneratorFrame frame = new BuddhabrotDataSourceGeneratorFrame();
                    frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public BuddhabrotDataSourceGeneratorFrame()
    {
        setResizable(false);
        setTitle("Mandelbrot Datasource Generator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 679, 412);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new BorderLayout(0, 0));
        
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        GridBagLayout gbl_panel = new GridBagLayout();
        gbl_panel.columnWidths = new int[] { 0, 0, 0, 0 };
        gbl_panel.rowHeights = new int[] { 0, 0, 0, 0 };
        gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
        panel.setLayout(gbl_panel);
        
        JLabel lblOutputFolder = new JLabel("Output folder");
        GridBagConstraints gbc_lblOutputFolder = new GridBagConstraints();
        gbc_lblOutputFolder.insets = new Insets(0, 0, 5, 5);
        gbc_lblOutputFolder.gridx = 0;
        gbc_lblOutputFolder.gridy = 0;
        panel.add(lblOutputFolder, gbc_lblOutputFolder);
        
        lblPath = new JLabel(".");
        GridBagConstraints gbc_lblPath = new GridBagConstraints();
        gbc_lblPath.insets = new Insets(0, 0, 5, 5);
        gbc_lblPath.gridx = 1;
        gbc_lblPath.gridy = 0;
        panel.add(lblPath, gbc_lblPath);
        
        JButton btnBrowse = new JButton("Browse");
        btnBrowse.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                selectOutputFolder();
            }
        });
        GridBagConstraints gbc_btnBrowse = new GridBagConstraints();
        gbc_btnBrowse.insets = new Insets(0, 0, 5, 0);
        gbc_btnBrowse.gridx = 2;
        gbc_btnBrowse.gridy = 0;
        panel.add(btnBrowse, gbc_btnBrowse);
        
        Component horizontalStrut = Box.createHorizontalStrut(500);
        GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
        gbc_horizontalStrut.insets = new Insets(0, 0, 5, 5);
        gbc_horizontalStrut.gridx = 1;
        gbc_horizontalStrut.gridy = 1;
        panel.add(horizontalStrut, gbc_horizontalStrut);
        
        JLabel lblPointsCount = new JLabel("Points count");
        GridBagConstraints gbc_lblPointsCount = new GridBagConstraints();
        gbc_lblPointsCount.insets = new Insets(0, 0, 0, 5);
        gbc_lblPointsCount.gridx = 0;
        gbc_lblPointsCount.gridy = 2;
        panel.add(lblPointsCount, gbc_lblPointsCount);
        
        JSpinner spinner = new JSpinner();
        spinner.addChangeListener(new ChangeListener()
        {
            public void stateChanged(ChangeEvent e)
            {
                if (e.getSource() instanceof JSpinner)
                {
                    JSpinner s = (JSpinner)e.getSource();
                    SpinnerNumberModel nm = ((SpinnerNumberModel)s.getModel());
                    updateConvertedLabel(nm.getNumber().longValue());
                }
            }
        });
        spinner.setModel(new SpinnerNumberModel(new Long(10000000), new Long(0), null, new Long(1000000)));
        GridBagConstraints gbc_spinner = new GridBagConstraints();
        gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
        gbc_spinner.insets = new Insets(0, 0, 0, 5);
        gbc_spinner.gridx = 1;
        gbc_spinner.gridy = 2;
        panel.add(spinner, gbc_spinner);
        
        lblConvert = new JLabel("1G");
        GridBagConstraints gbc_lblConvert = new GridBagConstraints();
        gbc_lblConvert.gridx = 2;
        gbc_lblConvert.gridy = 2;
        panel.add(lblConvert, gbc_lblConvert);
        
        JPanel buttonPanel = new JPanel();
        contentPane.add(buttonPanel, BorderLayout.SOUTH);
        
        JButton btnStart = new JButton("Start");
        buttonPanel.add(btnStart);
        
        JButton btnPause = new JButton("Pause");
        buttonPanel.add(btnPause);
        
        JButton btnStop = new JButton("Stop");
        buttonPanel.add(btnStop);
    }
    
    private void selectOutputFolder()
    {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int ret = fc.showOpenDialog(this);
        
        switch (ret)
        {
            case JFileChooser.APPROVE_OPTION:
                outputFolder = fc.getSelectedFile();
                lblPath.setText(outputFolder.getAbsolutePath());
                break;
            
            default:
                break;
        }
    }
    
    private void updateConvertedLabel(long number)
    {
        DecimalFormat df = new DecimalFormat("##0.#####E00");
        lblConvert.setText(df.format(number));
    }
}
